﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NewRetail.Application.Components.TianQue.Model;
using NewRetail.Application.Components.TianQue.Services;
using NewRetail.Application.Core;
using System;
using System.Threading.Tasks;

namespace NewRetail.Api.Controllers.WeChatControllers
{
    /// <summary>
    /// 天阙支付
    /// </summary>
    [NewRetailRoute("tianque-trade")]
    public class TianQueTradeController : BaseController
    {
        #region Field
        private readonly ITianQueService _tianQueService;
        private readonly ILogger _logger;
        #endregion

        #region Constructor
        public TianQueTradeController(ITianQueService tianQueService, ILogger<TianQueTradeController> logger)
        {
            _tianQueService = tianQueService;
            _logger = logger;
        }
        #endregion

        #region method

        #region 天阙POS订单收银台支付.生成订单
        /// <summary>
        /// 天阙POS订单收银台支付.生成订单
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost("trade-pos-pay")]
        public async Task<ResponseEntity<string>> TradePosPay([FromBody] PosOrderInput inputDto)
        {
            var ret = new ResponseEntity<string>();
            try
            {
                var result = await _tianQueService.SendTradePosPayOrderAsync(inputDto);
                if (!string.IsNullOrEmpty(result.Item2))
                    ret.SetError("发送订单失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("发送订单异常！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 天阙POS收银台订单状态查询
        /// <summary>
        /// 天阙POS收银台订单状态查询
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost("trade-online-pos-pay-query")]
        public async Task<ResponseEntity<OnlinePosOrderQueryResponse>> TradeOnlinePosPayQuery([FromBody] OnlinePosOrderQueryInput inputDto)
        {
            var ret = new ResponseEntity<OnlinePosOrderQueryResponse>();
            try
            {
                var result = await _tianQueService.QueryOnlineTradePosPayOrderAsync(inputDto);
                if (!string.IsNullOrEmpty(result.Item2))
                    ret.SetError("POS收银台订单状态查询失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("POS收银台订单状态查询异常！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 查询本地数据库中天阙支付结果情况
        /// <summary>
        /// 查询本地数据库中天阙支付结果情况
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost("trade-local-pos-pay-query")]
        public async Task<ResponseEntity<string>> TradeLocalPosPayQuery([FromBody] LocalPosOrderQueryInput inputDto)
        {
            var ret = new ResponseEntity<string>();
            try
            {
                var result = await _tianQueService.QueryLocalTradePosPayOrderAsync(inputDto);
                if (!string.IsNullOrEmpty(result.Item2))
                    ret.SetError("查询失败！" + result.Item2);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("查询异常！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #endregion
    }
}
